package server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;

/**
 * @author yujie
 * @createTime 2023/2/23 14:52
 * @description
 */
public class WebSocketServerInitializer extends ChannelInitializer<SocketChannel> {

    public static WebSocketFrameHandler webSocketFrameHandler = new WebSocketFrameHandler();

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new HttpServerCodec()); // http 编码
        pipeline.addLast(new HttpObjectAggregator(65536)); // http 消息聚合器
        pipeline.addLast(new WebSocketServerCompressionHandler()); // 压缩 可以不设置
        pipeline.addLast(new WebSocketServerProtocolHandler("/", null, true)); // 协议
        pipeline.addLast(webSocketFrameHandler); // 处理WebSocketFrame
    }
}
